<?php

class ActionPending_List extends BaseAction
{
    public function run()
    {
        if ( Yii::app()->request->getIsPostRequest())
        {
            $iTariffPlanId = Yii::app()->request->getParam('tariff_plan_id');
            $oTariffPlan = TariffPlan::model()->findByPk($iTariffPlanId);

            if($oTariffPlan)
            {
                $sDoRequest = Yii::app()->request->getParam('do');

                if ( $sDoRequest == 'approve' )
                {
                    $oTariffPlan->date_start = date('Y-m-d H:i:s');
                    $oTariffPlan->date_expired = date('Y-m-d H:i:s', strtotime('+30 day'));
                    $oTariffPlan->status = 'approved';
                    $oTariffPlan->save();

                    $oUser = User::model()->with('profile')->findByPk($oTariffPlan->user_id);

                    $oEmailNotification = new EmailNotification();
                    $oEmailNotification->sendEmailToUserAfterApproveTariffPlan($oUser, $oTariffPlan);

                    Yii::app()->user->setFlash('tariff_orders', 'Тарифный план утвержден');
                }

                if ( $sDoRequest == 'reject' )
                {
                    $oTariffPlan->status = 'rejected';
                    $oTariffPlan->save();

                    $oUser = User::model()->with('profile')->findByPk($oTariffPlan->user_id);

                    $oEmailNotification = new EmailNotification();
                    $oEmailNotification->sendEmailToUserAfterDeclineTariffPlan($oUser);

                    Yii::app()->user->setFlash('tariff_orders', 'Тарифный план отклонен');
                }
            }

            Yii::app()->controller->redirect(Yii::app()->controller->createUrl('/admin/tariff_orders/pending_list'));
        }

        $oCriteria = new CDbCriteria();
        $oCriteria->addColumnCondition(array('is_first' => 0, 'status' => 'pending'));
        $oCriteria->order = 'date_created DESC';

        $oDataProvider = new CActiveDataProvider('TariffPlan', array
        (
            'criteria'   => $oCriteria,
            'pagination' => array
            (
                'pageSize' => 100
            )
        ));

        $this->template->oDataProvider = $oDataProvider;
    }
}